﻿namespace MFDrivers.uLCD_32PT
{
    /// <summary>
    /// Object representing a set of coordinates
    /// </summary>
    public class Coord
    {
        /// <summary>
        /// x-axis Coordinate 
        /// </summary>
        public int X
        {
            set { this.XBytes = this.Convert(value); }
        }

        /// <summary>
        /// y-axis Coordinate 
        /// </summary>
        public int Y
        {
            set { this.YBytes = this.Convert(value); }
        }

        /// <summary>
        /// x-axis coordinate as 2 byte value
        /// </summary>
        public byte[] XBytes { get; private set; }
        
        /// <summary>
        /// y-axis coordinate as 2 byte value
        /// </summary>
        public byte[] YBytes { get; private set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="x">x-axis value</param>
        /// <param name="y">y-axis value</param>
        public Coord(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        /// <summary>
        /// Convert the specified int to a 2 byte value
        /// </summary>
        /// <param name="val">Value to be converted to bytes</param>
        /// <returns>2 Byte value of given int</returns>
        private byte[] Convert(int val)
        {
            if (val < 256)
                return new byte[] { 0x00, (byte)val };
            else
                return new byte[] { (byte)(val >> 8), (byte)(val & 255) };
        }
    }
}
